#include "optiontab.h"

OptionTab::OptionTab(QWidget *parent) :
  QWidget(parent)
{
  mainLayout = new QVBoxLayout(this);
  mainLayout->setMargin(10);

  firstBox = new IdBox(this);
  secondBox = new EventBox(this);

  mainLayout->addWidget(firstBox, 1, Qt::AlignTop);
  mainLayout->addWidget(secondBox, 2, Qt::AlignTop);
  setLayout(mainLayout);
}

OptionTab::~OptionTab()
{
}

OptionTab::IdBox::IdBox(QWidget *parent) :
  QGroupBox(parent)
{
  setTitle(tr("Chat's identification"));
  idBoxLayout.setMargin(10);

  regionLabel.setText(tr("Region:"));
  regionComboBox.addItems(Shard::regionStrList());
  firstLine.addWidget(&regionLabel, 1);
  firstLine.addWidget(&regionComboBox, 2);

  emailLabel.setText(tr("Email address:"));
  secondLine.addWidget(&emailLabel, 1);
  secondLine.addWidget(&emailTextBox, 2);

  passwordLabel.setText(tr("Password:"));
  passwordTextBox.setEchoMode(QLineEdit::Password);
  thirdLine.addWidget(&passwordLabel, 1);
  thirdLine.addWidget(&passwordTextBox, 2);

  authcodeLabel.setText(tr("Auth code:"));
  fourthLine.addWidget(&authcodeLabel, 1);
  fourthLine.addWidget(&authcodeTextBox, 2);

  connectButton.setText(tr("Connection"));

  connect(&emailTextBox, SIGNAL(returnPressed()), &connectButton, SLOT(click()));
  connect(&passwordTextBox, SIGNAL(returnPressed()), &connectButton, SLOT(click()));
  connect(&authcodeTextBox, SIGNAL(returnPressed()), &connectButton, SLOT(click()));

  // connect(&connectButton, SIGNAL(clicked()), ... session manager)

  idBoxLayout.addLayout(&firstLine);
  idBoxLayout.addLayout(&secondLine);
  idBoxLayout.addLayout(&thirdLine);
  idBoxLayout.addLayout(&fourthLine);
  idBoxLayout.addWidget(&connectButton);
  setLayout(&idBoxLayout);
}

OptionTab::EventBox::EventBox(QWidget *parent) :
  QGroupBox(parent)
{
  setTitle(tr("Event listener"));
  eventBoxLayout.setMargin(10);

  regionLabel.setText(tr("Region:"));
  regionComboBox.addItems(Shard::regionStrList());
  firstLine.addWidget(&regionLabel, 1);
  firstLine.addWidget(&regionComboBox, 2);

  shardLabel.setText(tr("Shard name:"));
  shardComboBox.addItems(Shard::shardStrList(0));
  secondLine.addWidget(&shardLabel, 1);
  secondLine.addWidget(&shardComboBox, 2);

  listenButton.setText(tr("Listen to this"));
  stopButton.setText(tr("Stop listening"));
  stopButton.setDisabled(true);
  thirdLine.addWidget(&listenButton, 1);
  thirdLine.addWidget(&stopButton, 1);

  eventBoxLayout.addLayout(&firstLine);
  eventBoxLayout.addLayout(&secondLine);
  eventBoxLayout.addLayout(&thirdLine);
  setLayout(&eventBoxLayout);

  connect(&regionComboBox, SIGNAL(currentIndexChanged(int)), parent, SLOT(eventRegionChanged(int)));
  connect(&listenButton, SIGNAL(clicked()), parent, SLOT(eventListenClicked()));
  connect(&stopButton, SIGNAL(clicked()), parent, SLOT(eventStopClicked()));
}

void OptionTab::eventRegionChanged(int reg)
{
  secondBox->shardComboBox.clear();
  secondBox->shardComboBox.addItems(Shard::shardStrList(reg));
}

void OptionTab::eventListenClicked()
{
  eventStopClicked();
  emit eventListenToIt(Shard::getShard(secondBox->regionComboBox.currentIndex(), secondBox->shardComboBox.currentIndex()));
}

void OptionTab::eventStopClicked()
{
  emit eventStopListening();
}

void OptionTab::eventListening(Shard s)
{
  secondBox->stopButton.setDisabled(false);
}

void OptionTab::eventStopping()
{
  secondBox->stopButton.setDisabled(true);
}
